import { _decorator, Collider2D, Component, Contact2DType, find, game, instantiate, IPhysics2DContact, Label, Node, NodePool, Prefab, RigidBody2D, v2, v3, Vec2 } from 'cc';
import { Monster } from './Monster';
import { Player } from './Player';
import 'minigame-api-typings'
import { re } from './re';
const { ccclass, property } = _decorator;

@ccclass('MainLlogic')
export class MainLlogic extends Component {

    @property({type:Node})
    private player:Node = null

    @property({type:[Prefab]})
    private monster:Prefab[] = []

    @property({type:[Prefab]})
    private updraftmonster:Prefab[] = []

    @property({type:Prefab})
    private curtain:Prefab = null

    @property({type:Prefab})
    private startScreen:Prefab = null

    @property({type:Prefab})
    private re:Prefab = null

    @property({type:Prefab})
    private jg:Prefab = null

    @property({type:Prefab})
    private curtainStart:Prefab = null

    @property({type:Prefab})
    private coin:Prefab = null

    createCoinDistance:number = 500

    /**
     * 刷怪倒计时，为0时刷怪
     */
    BrushMonsterTime:number = 120
    /**
     * 决定游戏是否运行
     */
    isRun:boolean = false

    /**
     * 游戏是否结束
     */
    gameover:boolean = false

    monsterpools:NodePool[] = []


    aaa:any = null

    recon:boolean = false

    // protected onLoad(): void {
    //     game.frameRate = 80
    // }

    start() {
        wx.getUserInfo({
            success(res){
            },
            fail(){
                let indosync = wx.getSystemInfoSync()
                let screenWidh = indosync.screenWidth
                let screenHeight = indosync.screenHeight
                const button = wx.createUserInfoButton({
                    type: 'text',
                    text: '获取用户信息',
                    style: {
                        left: 0,
                        top: 0,
                        width: screenWidh,
                        height: screenHeight,
                        lineHeight: 40,
                        backgroundColor: '#00000000',
                        color: '#ffffff',
                        textAlign: 'center',
                        fontSize: 0,
                        borderRadius: 4,
                        borderColor: '',
                        borderWidth: 0
                    }
                  })
                  button.onTap((res) => {
                    if(res.errMsg=="getUserInfo:ok"){
                        button.destroy()
                      }else{
                      }
                  })
            }
        })
    }

    createMonsterpools(){
        let monsterpool
        let i = 0
        if(this.player.getComponent(Player).updraft){
            for(i;i<this.updraftmonster.length;i++){
                monsterpool = new NodePool()
                monsterpool.put(instantiate(this.updraftmonster[i]))
                
                this.monsterpools.push(monsterpool)
                }
        }else{
            for(i;i<this.monster.length;i++){
                monsterpool = new NodePool()
                monsterpool.put(instantiate(this.monster[i]))
                
                this.monsterpools.push(monsterpool)
            }
        }
    }

    createjg(y:number){
        this.aaa = instantiate(this.jg)
        this.node.getParent().addChild(this.aaa)
        this.aaa.setPosition(this.player.position.x,y,0)
    }

    createcoin(){
        this.aaa = instantiate(this.coin)
        this.node.addChild(this.aaa)
        this.aaa.setPosition(this.player.position.x+800,this.getRandomInt(-200,0),0)
    }

    gameOver(){
        // wx.setUserCloudStorage({
        //     KVDataList:[{key:'score',value:this.node.getChildByName('score').getComponent(Label).string}],
        //     success(res){
        //         console.log(res)
        //     }
        // })
        wx.getOpenDataContext().postMessage({
            event:'updateScore',
            type:'engine',
            score:this.node.getChildByName('score').getComponent(Label).string
        })
        this.gameover = true
    }

    createStartScreen(){
        if(this.recon){
            this.aaa = instantiate(this.startScreen)
            this.node.getParent().addChild(this.aaa)
            this.aaa.setPosition(0,75,0)
        }
        this.aaa = instantiate(this.re)
        this.node.getParent().addChild(this.aaa)
        this.aaa.setPosition(0,-50,0)
    }

    /**
     * 幕布
     */
    curtainCame(){
        this.aaa = instantiate(this.curtain)
        this.node.addChild(this.aaa)
        this.aaa.setPosition(0,0,0)
    }

    curtainModeCame(){
        this.aaa = instantiate(this.curtainStart)
        this.node.addChild(this.aaa)
        this.aaa.setPosition(0,0,0)
    }

    /**
     * 开始游戏
     */
    curtainRose(){
        this.isRun = true
        this.gameover = false
        this.createMonsterpools()
    }

    /**
     * 复活
     */
    revive(){
        this.player.getComponent(Player).revive()
        this.recon = false
    }

    reset(){
        this.isRun = false
        this.gameover = false
        this.curtainCame()
    }


    getMonster(monsterpoolnum:number){
        if(this.monsterpools[monsterpoolnum].size()>0){
            this.aaa = this.monsterpools[monsterpoolnum].get()
            this.aaa.getComponent(Monster).monsterpoolnum = monsterpoolnum
            return this.aaa
        }else{
            if(this.player.getComponent(Player).updraft){
                this.monsterpools[monsterpoolnum].put(instantiate(this.updraftmonster[monsterpoolnum]))
                return this.getMonster(monsterpoolnum)
            }else{
                this.monsterpools[monsterpoolnum].put(instantiate(this.monster[monsterpoolnum]))
                return this.getMonster(monsterpoolnum)
            }
            
        }
        
    }

    /**
     * 回收怪物
     * @param monster 要回收的节点
     */
    putMonster(monster:Node){
        this.monsterpools[monster.getComponent(Monster).monsterpoolnum].put(monster)
    }

    /**
     * 刷怪方法，刷怪后重置倒计时
     */
    BrushMonster(){
        let i = this.getRandomInt(0,this.monsterpools.length-1)
        let node = this.getMonster(i)
        this.node.addChild(node)
        node.setPosition(v3(this.player.position.x+800,-260,0))
        node.getComponent(Monster).initialize(i)
        if(this.player.getComponent(Player).updraft){
            this.BrushMonsterTime = this.getRandomInt(30,90)
        }else{
            this.BrushMonsterTime = this.getRandomInt(60,120)
        }
        
    }

    getRandomInt(min: number, max: number): number {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }


    update(deltaTime: number) {
        if(Math.round(this.player.position.x/100) == this.createCoinDistance&&!this.recon){
            this.createcoin()
            this.createCoinDistance = this.createCoinDistance*3
        }
        if(!this.gameover){
            if(this.isRun){
                if(this.BrushMonsterTime > 0){
                    this.BrushMonsterTime--
                }else{
                    this.BrushMonster()
                }
            }
        }else{
            this.BrushMonsterTime = this.getRandomInt(60,120)
        }

    }
}


